//
//  XJ_NoticeViewController.m
//  XunJieProject
//
//  Created by xue on 2018/6/8.
//  Copyright © 2018年 Soowin. All rights reserved.
//

#import "XJ_NoticeViewController.h"
#import "XJ_NoticeTableViewCell.h"
#import "XJ_NoticeDetailViewController.h"
#import "XJ_NoticeListModel.h"

@interface XJ_NoticeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UITableView *myTableView;

@property (strong, nonatomic) NSMutableArray *dataArray;
@end

@implementation XJ_NoticeViewController
-(NSMutableArray*)dataArray{
    if (!_dataArray) {
        _dataArray =[[NSMutableArray alloc]init];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
      [self loadHomeAnnouncesList];
}
-(void)creatMainUI{
    
    self.nav_titleLabel.text =@"公告通知";
    
    //添加tableview
    _myTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-SafeAreaTopHeight-SafeAreaBottomHeight) style:(UITableViewStylePlain)];
    _myTableView.delegate=self;
    _myTableView.dataSource=self;
    _myTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    _myTableView.showsHorizontalScrollIndicator =NO;
    _myTableView.showsVerticalScrollIndicator =NO;
    _myTableView.backgroundColor =[UIColor clearColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
}

#pragma mark-UITableViewDelegate,UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count>0?_dataArray.count:0;
    
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * identifier = @"cell";
    XJ_NoticeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell == nil) {
        
        cell =[[[NSBundle mainBundle] loadNibNamed:@"XJ_NoticeTableViewCell" owner:nil options:nil]firstObject];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
        cell.backgroundColor =[UIColor clearColor];
    }
    XJ_NoticeListModel *model =_dataArray[indexPath.row];
    cell.model =model;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    XJ_NoticeListModel *model =_dataArray[indexPath.row];
    XJ_NoticeDetailViewController *detai =[[XJ_NoticeDetailViewController alloc]init];
    detai.model =model;
    [self.navigationController pushViewController:detai animated:YES];
}

#pragma mark--获取公告列表
-(void)loadHomeAnnouncesList{
    
    NSUserDefaults * db = [NSUserDefaults standardUserDefaults];
    NSString * token = [db objectForKey:@"token"];
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionary];
    [dic setValue:IDENTIFIER forKey:@"equipmentNo"];
    [dic setValue:STSTERMVERSION forKey:@"systemVersion"];
    [dic setValue:APPVERSION forKey:@"appVersion"];
    [dic setValue:USERID forKey:@"userId"];
    [dic setValue:token forKey:@"token"];
    
    
    [XJ_Utils signPost:@"/home/announces?" parameters:dic success:^(NSInteger recode, id jsonObject) {

        NSDictionary * dataDic = [jsonObject objectForKey:@"data"];
        
        NSArray * announceList = [dataDic objectForKey:@"announceList"];
        
        if (announceList.count > 0) {
            
            for (NSDictionary * dic in announceList) {
                
                XJ_NoticeListModel * model = [XJ_NoticeListModel mj_objectWithKeyValues:dic];
                
                [self.dataArray addObject:model];
            }
        }
        [self.myTableView reloadData];
        
        
    } failure:^(NSInteger recode, NSError *error) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
